package com.xiaoyu.client.ui.fragment.forum;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xiaoyu.client.ClientConstants;
import com.xiaoyu.client.R;
import com.xiaoyu.client.adapter.forum.ForumRecyAdapter;
import com.xiaoyu.client.model.forum.ForumBean;
import com.xiaoyu.client.model.forum.ForumParam;
import com.xiaoyu.client.network.NetworkManager;
import com.xiaoyu.client.ui.activity.forum.ForumDetailActivity;
import com.xiaoyu.client.ui.fragment.main.BillionCircleFragment;
import com.xiaoyu.commonlib.network.CommonHttpResponser;
import com.xiaoyu.commonlib.ui.OnItemRecyClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 亿友圈精选页面（即论坛）
 * Created by jk on 2018/10/18.
 */
public class ForumImgFragment extends Fragment {
    private View mView;
    private SmartRefreshLayout mRefreshLayout;
    private RecyclerView mRecyclerView;
    private String forumType;//精选:high, 文字：title， 图片:img, 视频:video, 最新:lately
    private int page = 1;//当前页数
    private int totalPage;//总页数
    private List<ForumParam> mList = new ArrayList<>();
    private ForumRecyAdapter mAdapter;



    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mView == null){
            mView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_forum,container,false);
            initView();
        }

        ViewGroup parent = (ViewGroup) mView.getParent();
        if (parent != null){
            parent.removeView(mView);
        }
        return mView;
    }

    @Override
    public void onResume() {
        super.onResume();
        page = 1;
        mList.clear();
        mAdapter.notifyDataSetChanged();
        getForumInfo();
    }

    private void initView() {
        mRefreshLayout = mView.findViewById(R.id.fragment_forum_refresh_layout);
        mRecyclerView = mView.findViewById(R.id.fragment_forum_list_layout);

        initRecy();

        mRefreshLayout.setOnRefreshListener(onRefreshListener);
        mRefreshLayout.setOnLoadMoreListener(onLoadMoreListener);
        mRefreshLayout.setNoMoreData(false);
    }

    private void initRecy() {
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(manager);
        mAdapter = new ForumRecyAdapter(getActivity(),mList);
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemRecyClickListener(onItemRecyClickListener);
    }

    /**
     * 列表item点击监听
     */
    private OnItemRecyClickListener onItemRecyClickListener = new OnItemRecyClickListener() {
        @Override
        public void onItemRecyClick(View view, int position) {
            Intent intent = new Intent(getActivity(), ForumDetailActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString(ClientConstants.FORUM_ID,mList.get(position).getForumid());
            intent.putExtras(bundle);
            startActivity(intent);
        }
    };


    /**
     * 列表刷新
     */
    private OnRefreshListener onRefreshListener = new OnRefreshListener() {
        @Override
        public void onRefresh(@NonNull RefreshLayout refreshLayout) {
            page = 1;
            mList.clear();
            mAdapter.notifyDataSetChanged();
            getForumInfo();
            mRefreshLayout.finishRefresh();
        }
    };

    /**
     * 加载更多
     */
    private OnLoadMoreListener onLoadMoreListener = new OnLoadMoreListener() {
        @Override
        public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
            if (page <= totalPage){
                page = page + 1;
                getForumInfo();
                mRefreshLayout.finishLoadMore();
                mRefreshLayout.setNoMoreData(false);
            }else {
                mRefreshLayout.finishLoadMore();
                mRefreshLayout.setNoMoreData(true);
                mRefreshLayout.finishLoadMoreWithNoMoreData();
                Toast.makeText(getActivity(),"已经没有更多数据！！！",Toast.LENGTH_LONG).show();
            }
        }
    };


    /**
     * 获取论坛数据
     */
    private void getForumInfo() {
        NetworkManager.getForumList(page, "img", new CommonHttpResponser() {
            @Override
            public void onSuccess(String result) {
                parseForumInfoSuc(result);
            }

            @Override
            public void onFailed(String errorCode, String errorDiscription) {
            }
        });
    }

    private void parseForumInfoSuc(String result) {
        if (page == 0){
            mList.clear();
        }
        ForumBean bean = new Gson().fromJson(result,ForumBean.class);
        mList.addAll(bean.getData().getList());
        totalPage = bean.getData().getCount();
        mAdapter.notifyDataSetChanged();
    }


}
